layui.use(['form','layer'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;
    var currNameData;
    var loadUpload;
    form.verify({
        fileName: function(value, item){ //value：表单的值、item：表单的DOM对象
            if (value == null || value > 255) return '文件名不能为空或者文件名过长';
            if(!new RegExp("[^\\s\\\\/:\\*\\?\\\"<>\\|](\\x20|[^\\s\\\\/:\\*\\?\\\"<>\\|])*[^\\s\\\\/:\\*\\?\\\"<>\\|\\.]$").test(value)){
                return '导出名中不能包含\\ / : * ? " < > | 中的任意字符,以及不能包含空白字符';
            }
        }
    });
    form.on("submit(addPhotoNameParam)",function(data){
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        $.post("/photoNameParamAdd", {
            xxId: $("#xxId").val(),
            photoName: $(".nameParam").val()
        }, function (data) {
            if (data =="success"||data.includes("success")) {
                top.layer.close(index);
                var msg = "添加成功!";
                msg = data.lastIndexOf("@@")!=-1?msg+data.split("@@")[1]:msg;
                top.layer.msg(msg);
                layer.closeAll("iframe");
                parent.location.reload();
            }else if (data == "error") {
                top.layer.msg("添加失败！");
            }else{
                top.layer.msg(data);
            }
        });
        return false;
    });

    $(".cancel-btn").on("click",function(){
        layer.closeAll("iframe");
        parent.location.reload();
    });

    function changeNameParam(){
        $.ajax({
            async: false,
            type: "POST",
            url: "/getNameParamDemo",
            data: "nameParam="+$("#nameParam").val(),
            before: function(){
                loadUpload = layer.load(1,{
                    shade:0.01
                });
            },
            success: function(data) {
                layer.close(loadUpload);
                //console.log(data);
                if('success' == data.msg){
                    $('.nameParamStr').val(data.nameParamStr);
                    $('.nameParamPre').val(data.nameParamPre);
                }else if('error' == data.msg){
                    layer.msg("操作失败。");
                }else{
                    layer.msg(data);
                }
            },
            error:function(XMLHttpRequest, textStatus){
                console.log(XMLHttpRequest);
                console.log(textStatus);
                layer.msg("保存失败，请联系管理员。");
            }
        });
    }

    function changeNameParamStr(){
        var paramStr = $(".nameParamStr").val();
        for(var i=0;i<paramArr.length;i++){
            paramStr = paramStr.replaceAll(paramArr[i],"@"+paramArr2[i]+"@");
        }
        $(".nameParam").val(paramStr);
        changeNameParam();
    }

    $(document).on('change','#nameParam',function(){
        changeNameParam();
    });
    $(document).on('change','.nameParamStr',function(){
        changeNameParamStr();
    });
    layer.config({
        extend: 'myskin/photo.css', //加载您的扩展样式
        //skin: 'nameParam_skin'
    });
    //$("#photoNameParamChoice").on("click",function(){
    $(document).on('click','#photoNameParamChoice',function(){
        var index=layer.open({
            title:'选择导出名参数',
            type:2,
            area: ['560px', '500px'],
            //area: ['80%', '100%'],
            //skin: "nameParam_skin",
            resize: false,
            content: ["/toSelectPhotoNameParam",'no'],
            btn: ['确定','关闭'],
            btnAlign: 'c',
            // shadeClose: true,
            yes: function(index,layerObj){
                //当点击‘确定'按钮的时候，获取弹出层返回的值
                var iframeWindow = layerObj.find('iframe')[0].contentWindow;
                currNameData = iframeWindow.layui.transfer.getData('nameParamTransfer');
                //console.log(currNameData);
                //[{"title":"学生姓名","value":"name"},{"title":"身份证","value":"sfz"},{"title":"学籍辅号","value":"xjfh"},{"title":"性别","value":"sex"},{"title":"当前长时间","value":"longDate"},{"title":"当前短时间","value":"shortDate"},{"title":"当前时间","value":"date"}]
                var titles = "",values = "";
                if(currNameData.length>0){
                    for(var i=0;i<currNameData.length;i++){
                        titles += currNameData[i].title+"_";
                        values += "@"+currNameData[i].value+"@_";
                    }
                    titles = titles!=''?titles.substring(0,titles.length-1):titles;
                    values = values!=''?values.substring(0,values.length-1):values;
                    //console.log(titles);
                    //console.log(values);
                    $(".nameParam").val(values);
                    $(".nameParamStr").val(titles);
                    $(".nameParamPre").val(values);
                    changeNameParam();
                    layer.close(index);
                }else{
                    layer.msg("必须要选择一个参数！");
                    layer.close(index);
                }
            },
            cancel: function(){
                layer.close(index);
            }
        });
    });
    //格式化时间
    function filterTime(val){
        if(val < 10){
            return "0" + val;
        }else{
            return val;
        }
    }
    //定时发布
    var time = new Date();
    var submitTime = time.getFullYear()+'-'+filterTime(time.getMonth()+1)+'-'+filterTime(time.getDate())+' '+filterTime(time.getHours())+':'+filterTime(time.getMinutes())+':'+filterTime(time.getSeconds());

})